//
//  YLSelectPhotoView.m
//  DEMOTest
//
//  Created by mac on 2020/5/9.
//  Copyright © 2020 mac. All rights reserved.
//

#import "TJSelectPhotoView.h"

#import "YLSelectPhotoCell.h"


@interface TJSelectPhotoView ()<UICollectionViewDataSource, UICollectionViewDelegate>


@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) UICollectionViewFlowLayout *flowLayout;


@end

static NSString * const selectPhotoCell = @"selectPhotoCell";



@implementation TJSelectPhotoView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        _datas = @[].mutableCopy;
 
        
        [self addSubview:self.collectionView];
        
        self.flowLayout.minimumLineSpacing = 15;
        self.flowLayout.minimumInteritemSpacing = 0;
        CGFloat W = (KScreenWidth - 32 - self.flowLayout.minimumInteritemSpacing * 5) / 3;
        
        self.flowLayout.itemSize = CGSizeMake(W, 120);
        self.collectionView.scrollEnabled = NO;
        self.collectionView.showsVerticalScrollIndicator = NO;
        self.collectionView.showsHorizontalScrollIndicator = NO;
        [self.collectionView registerClass:YLSelectPhotoCell.self forCellWithReuseIdentifier:selectPhotoCell];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.offset(0);
            make.top.offset(0);
            make.left.offset(0);
            make.right.offset(0);
        }];
 
    }
    return self;
}

- (UICollectionViewFlowLayout *)flowLayout {
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
    }
    return _flowLayout;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = UIColor.clearColor;
        _collectionView.delegate = self;
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _collectionView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YLSelectPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:selectPhotoCell forIndexPath:indexPath];
    TJSelectPhotoModel *model = self.datas[indexPath.row];
    cell.model = model;
    [cell setDidClickDeleteButtonBlock:^{
        if (self.didRemoveItemAtIndexPathBlock) {
            self.didRemoveItemAtIndexPathBlock(model);
        }
    }];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_didSelectItemAtIndexPathBlock) {
        _didSelectItemAtIndexPathBlock(self.datas[indexPath.row]);
    }
}

- (void)setDatas:(NSMutableArray *)datas {
    _datas = datas;
    [self.collectionView reloadData];
}

- (void)resetData {
    [self.collectionView reloadData];
}

@end
